﻿
//导入表定义
document.write("<script language='javascript' src='../../resources/js/views/commonFunction.js'></script>");
document.write("<script language='javascript' src='../../resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='../../resources/js/views/defineInit.js'></script>");
document.write("<script language='javascript' src='/resources/js/preAlarm/preAlarmCommon.js'></script>");

var action_area_county="/area/query/county.do";//获取区县信息
var action_self_view = "/area/query/self.do";
var action_area_allCity = "/area/query/selfCity.do"; //-- 城市
var action_county_depart="/department/query/county.do";//获取区县部门信息
var action_report_prealarm_area = "/report/prealarm/area.do";

var page_level="";
var city_html =
	'<label for="city_departmentSelect" class="pl20 pr10 spanstyle">请选择地市：</label>'
	+'<input name="city_departmentSelect" id="city_departmentSelect" class="w80 easyui-combobox" data-options="editable:false";>';
var county_html =
	'<label for="countySelect" class="pl20 pr10 spanstyle">请选择区县：</label>'
	+ '<input name="countySelect" id="countySelect" class="w150 easyui-combobox"  data-options="editable:false"; >';

var iHeight = document.documentElement.clientHeight-80;//判断表格自适应高度
var page = 1;
var pageSize = 200;


function query(curPage, pageSize) {
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	var reportShowColumns = getreportShowColumns();

	var departIds= $('#annualAnalysis_departmentSelect').combobox("getValues")+"";
	var indicatorType= $('#indicator_type_select').combobox("getValues")+"";
	indicatorType=indicatorType==""?"-1":indicatorType;
	var areaid= $('#countySelect').combobox("getValue");    //地域域编号
	areaid=areaid==""?"-1":areaid;
	$('#page').val(curPage);
	$('#pageSize').val(pageSize);
	var iname = $('#iname').textbox('getValue');

	var years = $('#comboboxYear').combobox('getText');
	var yearList = [];
	if(years!=null ||years!='' || years!=undefined){
		yearList=years.split(',');
	}

	var formid=$("#formid").combobox("getValue");
	$.get(action_report_prealarm_area, {"iname":iname, "departId": departIds, "indicatorType": indicatorType, "areaid": areaid, "years": years, "page": curPage, "pageSize": pageSize,"formsid":formid}, function (json) {
		var code=json.code;
	    if(code!=200){
			$("#view_containerzhuzi").height("0px");
			$("#view_containerzhuzi").width("0px");
            $('#tt').datagrid({data:[]});
			return;
	    }
        var data = json.data;
        var total = data["size"];
        if(total==""){
            $('#tt').datagrid({data:[]});
            $("#view_containerzhuzi").height("0px");
            $("#view_containerzhuzi").width("0px");
            return;
        }
        var totalPage = Math.ceil(total / pageSize);
        var start = (curPage - 1) * pageSize;
        var dataLength = 0;
        data = data["results"];
        if (data != null && data.length > 0) {
            dataLength = data.length;
        }

		$('#tt').datagrid( {
			frozen:true,
			fitColumns: true,
			striped: true,  //显示条纹，隔行换色
			columns: [reportShowColumns],
			singleSelect:true,// 是否单选
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [200, 500, 1000],
			rownumbers: true,
			pagination: true,//打开翻页功能
			sortable:true,
			sortName:"code",//排序
			multiSort:true, //定义是否允许多列排序
			remoteSort:false,//表示前端排序不请求服务器
			sortOrder:'asc',
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [200, 500, 1000],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
				query(curPage, pageSize);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		try{window.parent.unmask();}catch(err){}

		//画图
		if(dataLength>0){
            checkChangeImage(data, yearList);
		}

	}, "json");
}

//------------------------------------------------------------------菜单 start-------------------------------------------------------------
//获取当前用户所拥有的【地市+区县的区域信息】二级联动效果
function loadAreaView(){
	var param={}
	$.ajax({
		url:action_self_view,
		data:param,
		type: 'GET',
		dataType:"json",
		contentType:"application/json;charset=UTF-8",
		success: function (json) {
			var code = json.code;
			if(code!=200){
				alert("网络不好啊！");
				return;
			}
			var data = json.data;
			var level = data.level;
			if(level === null){
				alert("用户权限配置不合理，请联系系统管理员！");
				return;
			}
			page_level = level;

			//画页面
			//根据不同的用户权限，控制用户页面显示几级菜单
			if(level=="province"||level=="city"){
				$("#insert_html").html(city_html+county_html);
				loadAreaAllCity();
			}else if(level=="county"){
				$("#insert_html").html(county_html);
				//加载区县列表(有区县权限的用户)
				loadCounty(action_area_allCity, data.id);
			}
    	}
	});
}

//获取当前用户的【16地市区域】信息，每个用户可见16地市区域不一样，需要后端配置权限
function  loadAreaAllCity(){
	$.getJSON(action_area_allCity, function (json) {
		// json.data.unshift({
		// 	id : -1 ,
		// 	name : "全部"
		// });
		$('#city_departmentSelect').combobox({
			method : 'GET',
			async : false,
			data : json.data,
			editable : true,
			valueField : 'id',
			textField : 'name',
			onLoadSuccess : function() { // 数据加载完毕事件
				var a = $('#city_departmentSelect').combobox('getData');
				if (a.length > 0) {
					$('#city_departmentSelect').combobox('select', a[0].id);
				}
			},
			onSelect:function (res) {
				//选中某个地市：济南、青岛、德州等。
				loadCounty(action_area_county, res.id)
			}
		})
	},
	'json');
}

//获取当前用户的【区县区域】信息，每个用户可见区县区域不一样，需要后端配置权限
function loadCounty(action_url, id){
	$.get(action_url, {"areaid":id},function (json) {
		$('#countySelect').combobox({
			async:false,
			method:'GET',
			data:json.data,
			editable:true,
			valueField:'id',
			textField:'name',
			onLoadSuccess: function () {
				var a = $('#countySelect').combobox('getData');
				if (a.length > 0) {
				$('#countySelect').combobox('select', a[0].id);
				}
			},
			onSelect:function (city) {
				countyDepart(city.id)
			}
		})
	}, "json");
}

//加载区县部门信息
function countyDepart(id){
	$.get(action_county_depart + '?areaid='+id, function (json) {
		json.data.unshift({id: -1, name: "全部", sname: "全部"});
		var years = $('#comboboxYear').combobox('getText');
		var yearList = [];
		if(years!=null ||years!='' || years!=undefined){
			yearList=years.split(',');
		}
		$('#annualAnalysis_departmentSelect').combobox({
			async : false,
			method : 'GET',
			data : json.data,
			valueField : 'id',
			textField : 'name',
			fitColumns:false,
			collapsible: true,
			singleSelect : false,// 是否单选
			checkOnSelect: true,//选中行后,checkbox选中
			selectOnCheck: true,//选中行后,checkbox选中
			multiple:true,
			editable:true,
			formatter:function(row){
				var opts;
				if(row.selected==true){
					opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
				}else{
					opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
				}
				return opts;
			},
			onLoadSuccess: function () {
				var b = $('#annualAnalysis_departmentSelect').combobox('getData');
				if (b.length > 0) {
					$('#annualAnalysis_departmentSelect').combobox('select', b[0].id);
				}
			},
			onSelect:function () {
				query(page,pageSize);
				showImage((yearList.length)-1, "PAGETYPE_COUNTY");   //地市领域监测指标占比出图
			}
		});
	}, "json")
}

$(document).ready(function () {
	$("#tabDiv").height(iHeight+40);
	$(".panel-body").height('100%');
		$('#tablepanel').panel({
		title: '数据列表',
	});
	$('#tt').datagrid( {
		width : '100%',
		height : iHeight
	});

	loadYear();

	// // 页面加载之后再执行其他的！！！
	// window.onload = function (ev) {
		$('#tabDiv').tabs({
			height:700,
			onSelect:function(title,index){
				if(index==0){
				/*	var time= setTimeout(function(){

					},10);*/
				}else if(index==1){
					var time= setTimeout(function(){
						showData(series, jsonXData);
						showImage(0, "PAGETYPE_COUNTY")
					},10);
				}
			}
		});
		$("#tabDiv").width($("#content").width());
		loadIndicatorTypeAll();
		loadAreaView();
	// }
});

var CRUD = {
	queryList: function () {
		query(page,pageSize);
	}
};

/*
 * rgb颜色转换
 */
function zero_fill_hex(num, digits) {
  var s = num.toString(16);
  while (s.length < digits)
	s = "0" + s;
  return s;
}

function rgb2hex(rgb) {

  if (rgb.charAt(0) == '#')
	return rgb;

  var ds = rgb.split(/\D+/);
  var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
  return "#" + zero_fill_hex(decimal, 6);
}